package com.zmzncs.lmtc.module.shop.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zmzncs.lmtc.module.shop.dao.ShopTicketFileDao;
import com.zmzncs.lmtc.module.shop.pojo.shop_ticket.ShopTicket;
import com.zmzncs.lmtc.module.shop.dao.ShopTicketDao;
import com.zmzncs.lmtc.module.shop.pojo.shop_ticket.ShopTicketAddForm;
import com.zmzncs.lmtc.module.shop.pojo.shop_ticket.ShopTicketDTO;
import com.zmzncs.lmtc.module.shop.pojo.shop_ticket.ShopTicketSelectForm;
import com.zmzncs.lmtc.module.shop.pojo.shop_ticket_file.ShopTicketFile;
import com.zmzncs.lmtc.module.shop.service.ShopTicketService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 商户-优惠券 服务实现类
 * </p>
 *
 * @author 高博文
 * @since 2021-05-20
 */
@Service
public class ShopTicketServiceImpl extends ServiceImpl<ShopTicketDao, ShopTicket> implements ShopTicketService {

    @Autowired
    ShopTicketDao shopTicketDao;
    @Autowired
    ShopTicketFileDao shopTicketFileDao;


    @Transactional
    @Override
    public void add(ShopTicketAddForm form) {

        ShopTicket shopTicket = new ShopTicket();
        BeanUtils.copyProperties(form,shopTicket);
        shopTicket.setStatus(1);
        shopTicket.setIsDeleted(0);
        shopTicketDao.insert(shopTicket);

//        List<String> imageList = form.getImageList();
//        for (String url : imageList) {
//            ShopTicketFile file = new ShopTicketFile();
//            file.setTicketId(shopTicket.getId());
//            file.setFileUrl(url);
//            file.setIsDeleted(0);
//            shopTicketFileDao.insert(file);
//        }
    }

    @Override
    public IPage<ShopTicketDTO> getTicketPage(ShopTicketSelectForm form) {

        Page<Object> page = new Page<>(form.getCurrentPage(), form.getPageSize());
        IPage<ShopTicketDTO> iPage = shopTicketDao.getTicketPage(page,form);
         if (iPage != null && iPage.getRecords() != null && iPage.getRecords().size() > 0){
             for (ShopTicketDTO record : iPage.getRecords()) {
                 List<String> imageList = shopTicketFileDao.getfileByTicketId(record.getId());
                 if (imageList != null && imageList.size() > 0){
                     record.setImageUrl(imageList.get(0));
                 }
             }
         }
        return iPage;
    }

    @Override
    public List<ShopTicketDTO> getTicketByShopId(Long shopId) {
        return shopTicketDao.getTicketByShopId(shopId);
    }


}
